package com.feihong.yw189.activity.kuandai;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.feihong.yw189.R;
import com.feihong.yw189.activity.base.BaseActivity;
import com.feihong.yw189.adapter.ProjectAdapter;
import com.feihong.yw189.adapter.SpaceItemDecoration;
import com.feihong.yw189.app.App;
import com.feihong.yw189.listener.http.RequestCallBack;
import com.feihong.yw189.message.LoginMessage;
import com.feihong.yw189.model.Kaika.KuanDaiTcInfo;
import com.google.gson.Gson;

import org.xutils.common.Callback;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 2017/12/11
 * 宽带套餐
 */

public class KuanDaiTcActivity extends BaseActivity {

    @BindView(R.id.lv_kdtc)
    RecyclerView recyclerView;
    @BindView(R.id.kdtc_refresh_layout)
    SwipeRefreshLayout mSwipeRefreshLayout;

    @BindView(R.id.rl_goback)
    RelativeLayout ivBack;
    @BindView(R.id.txt_pagetitle)
    TextView txtPagetitle;
    @BindView(R.id.iv_lin)
    ImageView ivLin;
    @BindView(R.id.iv_confirm)
    TextView ivConfirm;
    @BindView(R.id.top_back_layout)
    LinearLayout topBackLayout;

    @BindView(R.id.connect_net_error_warning_tv)
    TextView mConnectErrorNetWarningTv;
    @BindView(R.id.connect_net_error_layout)
    LinearLayout mConnectNetErrorLayout;
    @BindView(R.id.null_data_layout)
    RelativeLayout mNullDataLayout;
    @BindView(R.id.null_data_layout_txt)
    TextView mNullDataWarningTv;
    private ProjectAdapter mProjectAdapter;
    private KuanDaiTcInfo.DataBean.ListBean listBean;
    private List<KuanDaiTcInfo.DataBean.ListBean.SpecListBean> spec_list = new ArrayList<>();
    private ArrayList<KuanDaiTcInfo.DataBean.ListBean.SpecListBean.SystemSpecInfoBean.ShowParmsBean.IsSulvBean> is_sulv = new ArrayList<>();
    private String isTel = "";
    private String isMin = "";
    private String isMax = "";
    private String specName = "";
    private String isPrice = "";
    private String dpPrice = "";
    private int isSulv = 0;
    private String groupId = "";
    private String specId = "";
    private int count = 0;
    private int totalNum = 0;
    private String space_g_id;
    private String phoneContNum;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_kdtc);
        ButterKnife.bind(this);

        initView();
        initData();
        initAdapter();
    }

    @Override
    public void setTheme() {

    }

    @Override
    public void onUserLogin(LoginMessage loginMessage) {

    }

    private void initView() {
        mNullDataWarningTv.setText("暂无套餐信息");
        mNullDataWarningTv.setTextColor(getResources().getColor(R.color.white));
        mConnectNetErrorLayout.setBackgroundColor(getResources().getColor(R.color.transparent));
        mConnectErrorNetWarningTv.setTextColor(getResources().getColor(R.color.white));
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        mSwipeRefreshLayout.setColorSchemeColors(getResources().getColor(R.color.appBlue));
        mSwipeRefreshLayout.setOnRefreshListener(() -> {
            // 刷新
            if (spec_list != null) {
                spec_list = new ArrayList<>();
            }
            getNet();
        });

        mSwipeRefreshLayout.setRefreshing(true);
        getNet();
    }

    private void getNet() {
//        App.getApp().getHttpTools().getKuanDaiTc(mRequestCallBack);
    }

    private void initData() {
        setPageTitle("宽带套餐");
        ivLin.setVisibility(View.GONE);
        ivBack.setOnClickListener(v -> finish());
    }

    private void initAdapter() {
        // 初始化Adapter
        mProjectAdapter = new ProjectAdapter(this);
        //设置item间距，30dp
        recyclerView.addItemDecoration(new SpaceItemDecoration(15));
        // 设置Adapter
        recyclerView.setAdapter(mProjectAdapter);
        // 设置点击事件
        mProjectAdapter.setOnItemClickListener(new ProjectAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {

                try {
                    // 宽带套餐点击事件
                    KuanDaiTcInfo.DataBean.ListBean.SpecListBean specListBean = spec_list.get(position);
                    KuanDaiTcInfo.DataBean.ListBean.SpecListBean.SystemSpecInfoBean.ShowParmsBean show_parms = specListBean.getSystem_spec_info().getShow_parms();

                    is_sulv = (ArrayList<KuanDaiTcInfo.DataBean.ListBean.SpecListBean.SystemSpecInfoBean.ShowParmsBean.IsSulvBean>) show_parms.getIs_sulv();

//                    totalNum = count + show_parms.getIs_tel();
                    // 座机个数
                    isTel = show_parms.getIs_tel() + "";
                    // 最少
                    isMin = show_parms.getIs_phone().getMin() + "";
                    // 最多
                    isMax = show_parms.getIs_phone().getMax() + "";
                    // 套餐名称
                    specName = specListBean.getSpec_name();
                    // 表面价格
                    isPrice = specListBean.getPrice();
                    // 真实价格
                    dpPrice = specListBean.getDp_price();
                    groupId = specListBean.getUser_group_id();
                    specId = specListBean.getSpec_id();
                    // 速率总数量
                    isSulv = is_sulv.size();
                    //组id
                    space_g_id = specListBean.getSpec_g_id();
                    itemJump();
                } catch (Exception e) {
                    showToast("数据错误");
                }
            }
        });
    }

    private void noDataLayout() {
        if (mNullDataLayout.getVisibility() == View.GONE) {
            mConnectNetErrorLayout.setVisibility(View.GONE);
            mSwipeRefreshLayout.setVisibility(View.GONE);
            mNullDataLayout.setVisibility(View.VISIBLE);
        }
    }

    private void connectNetErrorLayout() {
        if (mConnectNetErrorLayout.getVisibility() == View.GONE) {
            mSwipeRefreshLayout.setVisibility(View.GONE);
            mNullDataLayout.setVisibility(View.GONE);
            mConnectNetErrorLayout.setVisibility(View.VISIBLE);
        }
    }

    private void normalLayout() {
        if (mSwipeRefreshLayout.getVisibility() == View.GONE) {
            mNullDataLayout.setVisibility(View.GONE);
            mConnectNetErrorLayout.setVisibility(View.GONE);
            mSwipeRefreshLayout.setVisibility(View.VISIBLE);
        }
    }

    private RequestCallBack mRequestCallBack = new RequestCallBack<String>() {

        @Override
        public void onSuccess(String result) {
            if (activityIsDestory()) {
                return;
            }
            Gson gson = new Gson();
            try {
                KuanDaiTcInfo info = gson.fromJson(result, KuanDaiTcInfo.class);
                // KuanDaiTcInfo kaTcModel= JSON.parseObject(result,KuanDaiTcInfo.class);
                if (info != null) {
                    if (info.getStatus() == 1) {

                        List<KuanDaiTcInfo.DataBean.ListBean> list = info.getData().getList();
                        int index = 0;
                        ArrayList<String> taocanmingcheng = new ArrayList<>();
                        for (int i = 0; i < list.size(); i++) {
                            try {
                                listBean = list.get(i);
                                if (listBean.getSpec_list() != null && listBean.getSpec_list().size() > 0) {
                                    spec_list.addAll(listBean.getSpec_list());
                                    for (int j = 0; j < listBean.getSpec_list().size(); j++) {
                                        taocanmingcheng.add(listBean.getSpec_g_name());
                                    }
                                }
                            } catch (Exception ex) {
                            }
                        }
                        if (spec_list.size() > 0) {
                            normalLayout();
                            mProjectAdapter.setList(taocanmingcheng, spec_list);
                        } else {
                            noDataLayout();
                        }
                    } else {
                        if (spec_list == null || spec_list.size() <= 0) {
                            noDataLayout();
                        }
                        showToast(info.getInfo());
                    }
                } else {
                    noDataLayout();
                }
            } catch (Exception e) {
            }

        }

        @Override
        public void onError(Throwable ex, boolean isOnCallback) {
            if (activityIsDestory()) {
                return;
            }
            if (spec_list != null && spec_list.size() > 0) {
                showToast(getResources().getString(R.string.connect_sever_error_toast));
            } else {
                connectNetErrorLayout();
            }
        }

        @Override
        public void onCancelled(Callback.CancelledException cex) {
        }

        @Override
        public void onFinished() {
            if (mSwipeRefreshLayout != null) {
                mSwipeRefreshLayout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mSwipeRefreshLayout.setRefreshing(false);
                    }
                }, 300);
            }
        }
    };

    @OnClick(R.id.requery_data_tv)
    public void onClick() {
        getNet();
        mConnectNetErrorLayout.setVisibility(View.GONE);
    }

    public static final int KdRequestCode = 111;


    public void itemJump() {

        if (is_sulv != null && isSulv > 0) {
            Intent intent = new Intent();
            Bundle bundle = new Bundle();
            bundle.putString("Is_Tel", isTel);
            bundle.putString("Is_Phone_Min", isMin);
            bundle.putString("Is_Phone_Max", isMax);
            bundle.putString("Spec_Name", specName);
            bundle.putString("Is_Price", isPrice);
            bundle.putString("Dp_Price", dpPrice);
            bundle.putString("Group_ID", groupId);
            bundle.putString("Spec_ID", specId);
            bundle.putString("space_g_id", space_g_id);

            // 传递数组要实现Parcelable接口,不然会报错 IsSulvBean implements Parcelable
            bundle.putParcelableArrayList("Is_SuLv", is_sulv); // 速率
            intent.putExtras(bundle);
            setResult(KdRequestCode, intent);
            finish();
        } else {
            showToast("请先选择套餐");
        }
    }
}
